package com.ykq.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ykq.pojo.Emp;
import com.ykq.service.EmpService;
import com.ykq.util.UploadUtil;
import com.ykq.vo.EmpVo;
import com.ykq.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @program: vue_axios_ssm
 * @description:
 * @author: 闫克起2
 * @create: 2023-05-10 10:45
 **/
@Controller
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    @RequestMapping("/list")
    @ResponseBody //注解有效必须引入jackson的jar包
    public Result list() { //返回类型 是统一的json类对象
        List<Emp> emps = empService.queryAll();
        if (emps.size() == 0) {
            return new Result(500, "查询数据失败", null);
        }
        return new Result(200, "查询所有员工成功", emps);
    }


    @RequestMapping("/listByPage")
    @ResponseBody
    public Result listByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                             @RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<Emp> page= empService.queryAll(currentPage,pageSize);
       return new Result(200, "查询所有员工成功", page);
    }
    @RequestMapping("/listPageByCondition")
    @ResponseBody
    public Result listPageByCondition(@RequestParam(defaultValue = "1") Integer currentPage,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                                      @RequestBody EmpVo empVo
                                      ){
        System.out.println("============================"+empVo);
        PageInfo<Emp> page= empService.queryAll(currentPage,pageSize,empVo);
       return new Result(200, "查询所有员工成功", page);
    }



    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Integer id) {
        int row = empService.deleteById(id);
        if (row == 0) {
            return new Result(500, "删除失败", null);
        }
        return new Result(200, "删除成功", null);
    }

    @RequestMapping("insert")
    @ResponseBody  //把java对象转化为json对象
    public Result insert(@RequestBody Emp emp) { //RequestBody把前端的json对象转化为java对象
        int row = empService.save(emp);
        if (row == 0) {
            return new Result(500, "添加失败", null);
        }
        return new Result(200, "添加成功", null);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(@RequestBody Emp emp){
        int row= empService.update(emp);
        if (row == 0) {
            return new Result(500, "修改失败", null);
        }
        return new Result(200, "修改成功", null);
    }

    @RequestMapping("upload")
    @ResponseBody
    public Result upload(MultipartFile file){ //名称必须叫file
        String path = UploadUtil.upload(file);
        if(path==null){
            return new Result(500, "上传失败", null);
        }
        return new Result(200, "上传成功", path);
    }
}
